package com.yfp.goods.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import com.yfp.common.core.constant.DbConstants;
import com.yfp.goods.domain.ComboDetailImg;
import com.yfp.goods.domain.ImgResource;
import com.yfp.goods.domain.VideoResource;
import com.yfp.goods.mapper.ComboDetailImgMapper;
import com.yfp.goods.mapper.ImgResourceMapper;
import com.yfp.goods.mapper.VideoResourceMapper;
import com.yfp.goods.service.ComboDetailImgService;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 套餐详情图片表(ComboDetailImg)表服务实现类
 *
 * 
 * @since 2023-12-27 16:29:06
 */
@Service("comboDetailImgService")
@Slf4j
public class ComboDetailImgServiceImpl implements ComboDetailImgService {
    @Resource
    private ComboDetailImgMapper comboDetailImgMapper;

    @Resource
    private ImgResourceMapper imgResourceMapper;

    @Resource
    private VideoResourceMapper videoResourceMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ComboDetailImg queryById(Long id) {
        return this.comboDetailImgMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param comboDetailImg 筛选条件
     * @return 查询结果
     */
    @Override
    public List<ComboDetailImg> queryByPage(ComboDetailImg comboDetailImg) {
        return this.comboDetailImgMapper.queryAllByLimit(comboDetailImg);
    }

    /**
     * 新增数据
     *
     * @param comboDetailImg 实例对象
     * @return 实例对象
     */
    @Override
    public ComboDetailImg insert(ComboDetailImg comboDetailImg) {
        this.comboDetailImgMapper.insert(comboDetailImg);
        return comboDetailImg;
    }

    /**
     * 修改数据
     *
     * @param comboDetailImg 实例对象
     * @return 实例对象
     */
    @Override
    public ComboDetailImg update(ComboDetailImg comboDetailImg) {
        this.comboDetailImgMapper.update(comboDetailImg);
        return this.queryById(comboDetailImg.getId());
    }

    /**
     * 查询套餐图片
     *
     * @param comboId 组合id
     * @return {@link List}<{@link ComboDetailImg}>
     */
    @Override
    public List<ComboDetailImg> queryComboDetailImg(String comboId) {
        log.info("comboId:{}", comboId);
        List<ComboDetailImg> comboDetailImgs = this.comboDetailImgMapper.queryComboDetailImg(comboId);
        return comboDetailImgs.stream().map(goodsDetail -> {
            String resourceUrl = getResourceUrl(goodsDetail.getResourceType(), goodsDetail.getResourceId());
            if (CharSequenceUtil.isNotEmpty(resourceUrl)) goodsDetail.setResourceUrl(resourceUrl);
            return goodsDetail;
        }).toList();

    }

    public String getResourceUrl(Integer resourceType, String resourceId) {
        if (CharSequenceUtil.isEmpty(resourceId)) return null;

        if (Objects.equals(DbConstants.machineCorrelation.AD_TYPE_IMG, resourceType)){
            ImgResource imgResource = this.imgResourceMapper.queryById(resourceId);
            return imgResource.getUrl();
        }
        if (Objects.equals(DbConstants.machineCorrelation.AD_TYPE_VIDEO, resourceType)){
            VideoResource videoResource = this.videoResourceMapper.queryById(resourceId);
            return videoResource.getUrl();
        }
        return null;
    }

    @Override
    public boolean delete(String id) {
        return this.comboDetailImgMapper.delete(Long.valueOf(id));
    }
}
